package org.opensuse.bs.ui.model;

import org.eclipse.jface.viewers.AbstractTreeViewer;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.ui.model.WorkbenchContentProvider;
import org.eclipse.ui.progress.DeferredTreeContentManager;

public class RemoteContentProvider extends WorkbenchContentProvider {
	private DeferredTreeContentManager manager;
	protected TreeViewer viewer;

	public Object[] getChildren(Object element) {
		if(manager != null) {
			Object[] children = manager.getChildren(element);
			if(children != null) {
				return children;
			}
		}
		return super.getChildren(element);
	}

	public Object getParent(Object element) {
		if(element instanceof RootElement)
			return null;
		else
			return ((BSModelElement) element).getParent(element);
	}

	public boolean hasChildren(Object element) {
		if(manager != null) {
			return manager.mayHaveChildren(element);
		}
		return super.hasChildren(element);
	}

	public Object[] getElements(Object inputElement) {
		return getChildren(inputElement);
	}

	public void dispose() {}

	public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
		if (viewer instanceof AbstractTreeViewer) {
		    manager = new DeferredTreeContentManager(this, (AbstractTreeViewer) viewer);
		}
		super.inputChanged(viewer, oldInput, newInput);
	}

}
